
const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    colorTheme: app.globalData.colorTheme,
    teamId: null,
    recruitmentId: null,
    roleList: [],
    checkbox: [],
    animationTime: 1,
    userInfo: null
  },

  /**
   * 获取用户主页信息
   */
  getUserPageInfo: function(uid) {
    let that = this;
    let url = config.SERVER_URL + 'index/user/' + uid;
    wx.showLoading({
      title: ''
    })
    request.requestGetApi(url, {}, {}, 'application/json').then(res => {
      if (res.status === config.STATUS_OK) {
        let roleId = res.data.roles;
        let roleList = app.roleTagsBuild(roleId);
        this.setData({
          userInfo: res.data,
          roleList: roleList
        });
      }
    }).catch(err => {
      app.failFun(err)
    }).finally((res) => {
      app.completeFun(res)
    });
  },

  /**
   * 获取队伍列表成功回调
   */
  listTeamsSucc: function(res, selfObject) {
    if (res.status != 200) {
      app.failFun(res, selfObject);
      return;
    }
    this.setData({
      checkbox: res.data,
    });
    this.showModal({
      currentTarget: {
        dataset: {
          target: 'SelectorModal'
        }
      }
    })
  },

  /**
   * 获取职位列表成功回调
   */
  listRecruitmentsSucc: function(res, selfObject){
    if(res.status != 200){
      app.failFun(res, selfObject);
      return;
    }
    console.log(res.data);
    this.setData({
      checkbox: res.data
    });
    this.showModal({
      currentTarget: {
        dataset: {
          target: 'SelectorModal'
        }
      }
    });
  },

  /**
   * 发出邀请成功回调
   */
  inviteSucc: function(res, selfObject){
    if(res.status != 200){
      app.failFun(res, selfObject);
      return;
    }
    this.hideModal();
    wx.showToast({
      title: '邀请成功',
      icon: 'success',
      duration: 3000
    });
  },

  /**
   * 模态框
   */
  showModal(e) {
    let modalName = e.currentTarget.dataset.target;
    if (modalName == 'Image') {
      this.setData({
        imageUrl: e.currentTarget.dataset.imageurl
      });
    }
    this.setData({
      modalName: modalName
    })
  },
  hideModal() {
    this.setData({
      modalName: null,
      teamId: null,
      recruitmentId: null
    })
  },

  /**
   * 点中模态框的确认按钮
   */
  confirm: function(e) {
    let that = this;
    let teamId = this.data.teamId;
    let recruitmentId = this.data.recruitmentId;
    if(teamId === null) return;
    if (recruitmentId == null){
      // 加载职位信息并弹框
      wx.showLoading({
        title: '加载ing'
      });
      this.hideModal();
      this.data.teamId = teamId;
      let url = config.SERVER_URL + 'index/recruitments/' + teamId;
      request.requestGetApi(url, {}, {}, 'application/json').then(res=>{
        that.listRecruitmentsSucc(res, that);
      }).catch(err=>{
        app.failFun(err);
      }).finally(res=>{
        app.completeFun(res);
      })
    }else{
      // 发送邀请
      let url = config.SERVER_URL + 'teams/teamInvitesUser';
      let params = {
        teamId: teamId,
        recruitmentId: recruitmentId,
        uid: this.data.userInfo.uid
      }
      let token = wx.getStorageSync('token');
      wx.showLoading({
        title: '发送邀请ing'
      });
      request.requestPostApi(url,token,params,'application/json').then(res=>{
        that.inviteSucc(res, that);
      }).catch(err=>{
        app.failFun(err);
      }).finally(res=>{
        app.completeFun(res);
      })
    }
  },

  /**
   * 点中标签触发
   */
  chooseTag: function(e) {
    let id = e.currentTarget.dataset.value;
    let allTags = this.data.checkbox;
    let teamId = this.data.teamId;
    allTags.find(function(value, index) {
      allTags[index].checked = false;
      if (id == value.id) {
        allTags[index].checked = !allTags[index].checked;
      }
    });
    this.setData({
      checkbox: allTags,
      teamId: e.currentTarget.dataset.teamid?teamId:id,
      recruitmentId: e.currentTarget.dataset.teamid?id:null
    });
  },

  /**
   * 打开选择队伍/职位的模态框
   */
  showSelectorModal: function(e) {
    if(!app.checkedUserLoading()){
      return;
    }
    let that = this;
    let teamId = this.data.teamId;
    let uid = wx.getStorageSync('userInfo').uid;
    wx.showLoading({
      title: '加载ing'
    })
    console.log(uid);
    let url = config.SERVER_URL + 'index/teams/' + uid;
    request.requestGetApi(url, {}, {}, 'application/json').then(res=>{
      that.listTeamsSucc(res, that);
    }).catch(err=>{
      app.failFun(err);
    }).finally(res=>{
      app.completeFun(res);
    })
  },

  openChat: function(e) {
    let target_uid = e.currentTarget.dataset.uid;
    wx.navigateTo({
      url: '/pages/chat-room/chat-room?target_uid=' + target_uid,
    })
  },

  /**
   * 图片放大
   */
  preview: function(e) {
    let imageUrl = e.currentTarget.dataset.imageurl;
    wx.previewImage({
      current: imageUrl, // 当前显示图片的http链接
      urls: [imageUrl] // 需要预览的图片http链接列表
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserPageInfo(options.uid);
    // this.getUserPageInfo(21);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})